package com.easy.auth.service.impl;

import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.easy.auth.domain.Userinfo;
import com.easy.auth.service.UserinfoService;
import com.easy.base.service.impl.EManagerImpl;
import com.easy.common.jdbc.DymaticCondition;
import com.easy.common.jdbc.PageInfo;
import com.easy.common.util.BeanUtil;
import com.easy.common.web.PagedList;

@Transactional
@Service
public class UserinfoServiceImpl extends EManagerImpl<Userinfo> implements
		UserinfoService {

	@Override
	public int removeUser(String oids) {
		int count = 0;
		String[] userIds = oids.split("-");
		String sql = "DELETE FROM userinfo_role WHERE userid = ?";
		if (userIds != null) {
			for (String userid : userIds) {
				super.update(sql, userid);
				Userinfo userinfo = new Userinfo();
				userinfo.setOid(userid);
				count += super.remove(userinfo);
			}
		}

		return count;
	}

	@Override
	public Userinfo findUserinfo(Userinfo userinfo) {
		return super.find(userinfo);
	}

	@Override
	public PagedList<Userinfo> findAllUserinfo(Userinfo userinfo,
			PageInfo pageInfo) {
		return super.findAllLike(userinfo, pageInfo);
	}

	@Override
	public int saveOrEditUserinfo(Userinfo userinfo) {
		return super.saveOrEdit(userinfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagedList<Userinfo> getDepHaveUsersByUserid(String depid,
			PageInfo pageInfo) {
		String sql = "SELECT user.* FROM userinfo user, userinfo_department userdept "
				+ "WHERE 1=1 and user.oid = userdept.userid ";
		DymaticCondition dymaticCondition = new DymaticCondition();
		dymaticCondition.addCondition("AND userdept.departmentid = '?'", depid);

		return super.query(
				sql,
				dymaticCondition,
				new BeanPropertyRowMapper<Userinfo>(BeanUtil.getGenericType(
						this, 0)), pageInfo);
	}

	@SuppressWarnings("unchecked")
	@Override
	public PagedList<Userinfo> getDepNotHaveUsersByUserId(String depid,
			PageInfo pageInfo) {
		String sql = "SELECT * FROM userinfo WHERE 1= 1 ";
		DymaticCondition dymaticCondition = new DymaticCondition();
		dymaticCondition
				.addCondition(
						"AND oid NOT IN(SELECT user.oid FROM userinfo user, userinfo_department userDept WHERE userDept.userid = user.oid and userDept.departmentid = '?')",
						depid);

		return super.query(
				sql,
				dymaticCondition,
				new BeanPropertyRowMapper<Userinfo>(BeanUtil.getGenericType(
						this, 0)), pageInfo);
	}

	@Override
	public int removeDepartmentUsers(String depid, String[] userids) {
		int count = 0;
		if (userids != null) {
			for (String userid : userids) {
				String sql = "DELETE FROM userinfo_department WHERE departmentid = ? and userid = ?";
				count += super.update(sql, depid, userid);
			}
		}

		return count;
	}

	@Override
	public int addDepartmentUsers(String depid, String[] userids) {
		int count = 0;
		if (userids != null) {
			for (String userid : userids) {
				String sql = "INSERT INTO userinfo_department VALUES(?, ?)";
				count += super.update(sql, userid, depid);
			}
		}

		return count;
	}

}
